package com.hqyj.onlineExamination.modules.exam.service.impl;


import com.hqyj.onlineExamination.modules.common.entity.ResultEntity;
import com.hqyj.onlineExamination.modules.common.entity.SubmitPaperBean;
import com.hqyj.onlineExamination.modules.exam.dao.AchievementDao;
import com.hqyj.onlineExamination.modules.exam.dao.AnswerDao;
import com.hqyj.onlineExamination.modules.exam.dao.PaperDao;
import com.hqyj.onlineExamination.modules.exam.dao.QuestionDao;
import com.hqyj.onlineExamination.modules.exam.entity.*;
import com.hqyj.onlineExamination.modules.exam.service.AchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AchievementServiceImpl implements AchievementService {

    @Autowired
    private PaperDao paperDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private AchievementDao achievementDao;
    @Autowired
    private AnswerDao answerDao;


    @Override
    @Transactional
    public ResultEntity<Achievement> insertAchievement(SubmitPaperBean submitPaperBean) {
        //创建一个Achievement对象
        Achievement achievement = new Achievement();
        //拿到考生id和实际用时并封装到Achievement中
        achievement.setUserId(submitPaperBean.getUserId());
        achievement.setSpendTime(submitPaperBean.getSpendTime());
        //拿到试卷id并查询出试卷的内容
        Paper paper = paperDao.getPaperById(submitPaperBean.getPaperId());
        //封装
        achievement.setSubject(paper.getSubject());
        achievement.setTotalTime(paper.getTotalTime());
        achievement.setTotalScore(paper.getTotalScore());
        //参考得分
        Double rScore = 0d;
        //循环遍历answers
        for (Map.Entry<Integer, Object> it : submitPaperBean.getUserAnswer().entrySet()) {
            int questionId = it.getKey();
            //查询这个问题
            Question question = questionDao.selectQuestionById(questionId);
            //单择题.多选题，判断题得分
            if(question.getType().equals(QuestionType.SINGLE_CHOICE.name)){
                if(question.getReferenceAnswer().equals(it.getValue())){
                    rScore += question.getScore();
                }
            }else if(question.getType().equals(QuestionType.MULTIPLE_CHOICE.name)){
                if(question.getReferenceAnswer().equals(it.getValue())){
                    rScore += question.getScore();
                }
            }else if(question.getType().equals(QuestionType.JUDGE.name)){
                if(question.getReferenceAnswer().equals(it.getValue())){
                    rScore += question.getScore();
                }
            }
        }
        //封装参考得分
        achievement.setReferenceScore(rScore.toString());
        //老师没有批阅之前分数皆为-1
        achievement.setScore(-1d);
        achievement.setCreateDate(LocalDateTime.now());
        //插入数据库
        achievementDao.insertAchievement(achievement);
        //重新遍历循环，将考生答案录入
        for (Map.Entry<Integer, Object> it : submitPaperBean.getUserAnswer().entrySet()) {
            Answer answer = new Answer();
            answer.setCreateDate(LocalDateTime.now());
            answer.setAchievementId(achievement.getId());
            answer.setQuestionId(it.getKey());
            answer.setUserAnswer((String) it.getValue());
            answerDao.insertAnswer(answer);
        }
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"交卷完成！",achievement);
    }

    //初始化改卷数据
    public ResultEntity<Map<String,Object>> getAchievementById(int id) {
        //创建一个map集合
        Map<String,Object> map = new HashMap<>();
        //查询数据库
        List<Answer> answers = answerDao.getAnswersByAchievementId(id);
        if(answers==null){
            return new ResultEntity<Map<String,Object>>(ResultEntity.ResultStatus.FAILED.status,"没有这张试卷！");
        }
        //创建5个list集合来装不同的题型
        List<Question> listSingleChoice = new ArrayList<>();
        List<Question> listMultipleChoice = new ArrayList<>();
        List<Question> listJudge = new ArrayList<>();
        List<Question> listFillBlank = new ArrayList<>();
        List<Question> listShortAnswer = new ArrayList<>();
        List<Question> listProgramming = new ArrayList<>();
        //遍历循环answers
        for(Answer answer : answers){
            Question question = questionDao.selectQuestionById(answer.getQuestionId());
            if(question.getType().equals(QuestionType.SINGLE_CHOICE.name)){
                listSingleChoice.add(question);
            }else if(question.getType().equals(QuestionType.MULTIPLE_CHOICE.name)){
                listMultipleChoice.add(question);
            }else if(question.getType().equals(QuestionType.JUDGE.name)){
                listJudge.add(question);
            }else if(question.getType().equals(QuestionType.FILL_BLANK.name)){
                listFillBlank.add(question);
            }else if(question.getType().equals(QuestionType.SHORT_ANSWER.name)){
                listShortAnswer.add(question);
            }else if(question.getType().equals(QuestionType.PROGRAMMING.name)){
                listProgramming.add(question);
            }

        }

        //将这些list全部装入map返回给前端
        map.put(QuestionType.SINGLE_CHOICE.name,listSingleChoice);
        map.put(QuestionType.MULTIPLE_CHOICE.name,listMultipleChoice);
        map.put(QuestionType.JUDGE.name,listJudge);
        map.put(QuestionType.FILL_BLANK.name,listFillBlank);
        map.put(QuestionType.SHORT_ANSWER.name,listShortAnswer);
        map.put(QuestionType.PROGRAMMING.name,listProgramming);
        map.put("userAnswers",answers);
        return new ResultEntity<Map<String,Object>>(ResultEntity.ResultStatus.SUCCESS.status,"查询成功！",map);

    }

    public ResultEntity correction(Achievement achievement){
        achievementDao.updateScoreById(achievement);
        return new ResultEntity(ResultEntity.ResultStatus.SUCCESS.status,"批阅成功！");
    }
}
